package com.pai4j.pgc.service.queue;
import com.pai4j.common.constants.MessageConstants;
import com.pai4j.common.util.JsonUtil;
import com.pai4j.domain.vo.dto.FocusMessageDTO;
import com.pai4j.domain.vo.dto.BaseMessageDTO;
import com.pai4j.domain.vo.dto.CommentReplyMessageDTO;
import com.pai4j.domain.vo.dto.LikeCollectMessageDTO;
import com.pai4j.domain.vo.dto.SystemMessageDTO;
import com.pai4j.pgc.service.message.MessageService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import com.pai4j.common.enums.RedisKeyEnum;

/**
 *  消息中心消息队列
 *  *设置@Lazy为false，因为server这边默认bean懒加载，很有可能一开始就执行不了初始化方法
 *
 * @author: CYM-pai
 * @date: 2025/10/27 20:41
 **/
@Component
@Lazy(value = false)
@Slf4j
public class UserMessageQueue extends AbstractRedisQueue<BaseMessageDTO> {

    @Autowired
    private MessageService messageService;

    @PostConstruct
    public void initBean() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                UserMessageQueue.this.schedule();
            }
        }).start();
    }

    @Override
    protected int getPoolSize() {
        return 3;
    }

    /**
     * 从消息队列redis读取数据的超时时间
     * @return
     */
    @Override
    protected long getTimeout() {
        return 10000;
    }

    /**
     * 一个任务元素重试的次数
     * @return 默认不重试
     */
    @Override
    protected int getRetryTimes() {
        return 1;
    }

    /**
     * 一个任务最多被重返队列的次数
     * @return
     */
    @Override
    protected int getBackQueueTimes() {
        return 0;
    }

    @Override
    protected BaseMessageDTO convert(String s) {
        BaseMessageDTO baseMessage = JsonUtil.fromJson(s, BaseMessageDTO.class);
        if (MessageConstants.CategoryEnum.SYSTEM.getCategory().equals(baseMessage.getCategory())) {
            return JsonUtil.fromJson(s, SystemMessageDTO.class);
        } else if (MessageConstants.CategoryEnum.LIKE_COLLECT.getCategory().equals(baseMessage.getCategory())) {
            return JsonUtil.fromJson(s, LikeCollectMessageDTO.class);
        } else if (MessageConstants.CategoryEnum.COMMENT_REPLY.getCategory().equals(baseMessage.getCategory())) {
            return JsonUtil.fromJson(s, CommentReplyMessageDTO.class);
        } else if (MessageConstants.CategoryEnum.FOCUS.getCategory().equals(baseMessage.getCategory())) {
            return JsonUtil.fromJson(s, FocusMessageDTO.class);
        }
        return null;
    }

    @Override
    protected String getQueueName() {
        return RedisKeyEnum.QUEUE_WEB_USER_MESSAGE.getKey();
    }

    @Override
    protected void receive(BaseMessageDTO data) {
        if (data == null) {
            return;
        }
        if (data.getToUserId().equals(data.getFromUserId())) {
            return;
        }
        log.info("【用户消息队列】开始处理消息, taskId:{}, message:{}", data.getTaskId(), data);
        messageService.send(data);
        log.info("【用户消息队列】消息处理成功. taskId:{}", data.getTaskId());
    }

    @Override
    protected void error(BaseMessageDTO data, Exception ex) {
        log.error("【用户消息队列】消费出错 taskId:{}, ex:{}", data.getTaskId(), ex);
    }
}
